# coding=utf-8
import numpy as np
import pymongo



class Searcher(object):
    def __init__(self, queryFeatures):
        self.results = {}
        self.queryFeatures = queryFeatures

    def chi2_distance(self, histA, histB, eps=1e-10):
        # compute the chi-squared distance
        d=0
        for i in range(len(histA)):
            a = histA[i]
            b = histB[i]
            d += 0.5 * (((a - b) ** 2) / (a + b + eps))  # return the chi-squared distance
        return d

    # 生产者
    def Search(self):
        # 数据库连接
        db = pymongo.MongoClient()
        index = db.features.tindex
        tindex = index.find()
        L = []
        L.extend(tindex)
        # 协程处理

        for feature in L:
            s=feature['_id']
            features=feature[s]
            d = self.chi2_distance(features, self.queryFeatures)
            self.results[s] = d
        # 结果排序
        results = sorted([(v, k) for (k, v) in self.results.items()], reverse=True)

        self.results = results[0:6]


'''class Searcher:
    def __init__(self, indexPath):
        # store our index path
        self.indexPath = indexPath
    def chi2_distance(self, histA, histB, eps=1e-10):
        # compute the chi-squared distance
        d = 0.5 * np.sum([((a - b) ** 2) / (a + b + eps)
                          for (a, b) in zip(histA, histB)])

        # return the chi-squared distance
        return d

    def search(self, queryFeatures, limit=10):
        # initialize our dictionary of results
        results = {}

        # open the index file for reading
        with open(self.indexPath) as f:
            # initialize the CSV reader
            reader = csv.reader(f)

            # loop over the rows in the index
            for row in reader:
                # parse out the image ID and features, then compute the
                # chi-squared distance between the features in our index
                # and our query features
                features = [float(x) for x in row[1:]]
                d = self.chi2_distance(features, queryFeatures)

                # now that we have the distance between the two feature
                # vectors, we can udpate the results dictionary -- the
                # key is the current image ID in the index and the
                # value is the distance we just computed, representing
                # how 'similar' the image in the index is to our query
                results[row[0]] = d

            # close the reader
            f.close()

        # sort our results, so that the smaller distances (i.e. the
        # more relevant images are at the front of the list)
        results = sorted([(v, k) for (k, v) in results.items()])

        # return our (limited) results
        return results[:limit]'''